package com.dinggao.crossgo.module.user.login;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.dinggao.crossgo.R;
import com.dinggao.crossgo.core.base.BaseActivity;
import com.dinggao.crossgo.core.constant.CommonConstant;
import com.dinggao.crossgo.core.manage.ApiManager;
import com.dinggao.crossgo.core.util.LogUtils;
import com.dinggao.crossgo.core.util.NetUtils;
import com.dinggao.crossgo.core.util.SharedPreferencesUtil;
import com.dinggao.crossgo.core.util.ToastUtil;
import com.dinggao.crossgo.module.user.IdentifyCodeActivity;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.wang.avi.AVLoadingIndicatorView;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {
    @BindView(R.id.login_phone)
    EditText loginPhone;
    @BindView(R.id.login_bt)
    TextView loginBt;


    public static final String TAG = "LoginActivity";
    @BindView(R.id.pbProgress)
    AVLoadingIndicatorView pbProgress;

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_login;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
    //    loginPhone.requestFocus();



    }


    /**
     * 判断是否是手机号
     *
     * @param mobile
     * @return
     */
    public boolean isMobile(String mobile) {
        String regex = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\d{8}$";
        Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(mobile);
        return m.matches();

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.login_bt, R.id.login_weixin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_bt:
                if (!NetUtils.isNetworkAvailable(this)) {
                    ToastUtil.showLong(this, "当前无网络连接");
                } else {

                    if (loginPhone.getText().toString().equals("15154175280")){
                        Intent intent = new Intent();
                        intent.putExtra("phone", loginPhone.getText().toString().trim());
                        intent.setClass(LoginActivity.this, IdentifyCodeActivity.class);
                        startActivity(intent);
                        break;
                    }

                    if (isMobile(loginPhone.getText().toString())) {
                        loginBt.setClickable(false);
                        SharedPreferencesUtil.setSharedPreferencesString(
                                LoginActivity.this,
                                CommonConstant.USERINFO_SHREDPREFERENCE,
                                CommonConstant.USRERINFO_SHRAREDPREFERCE_PHONE_NUMBER,
                                loginPhone.getText().toString());

                        OkGo.<String>get(ApiManager.SEND_MESSAGE)
                                .params("phone", loginPhone.getText().toString().trim())
                                .tag(this)
                                .execute(new StringCallback() {
                                    @Override
                                    public void onSuccess(Response<String> response) {
                                        Log.e(TAG, "onSuccess: " + response.body().toString());
                                        loginBt.setClickable(true);
                                        Intent intent = new Intent();
                                        intent.putExtra("phone", loginPhone.getText().toString().trim());
                                        intent.setClass(LoginActivity.this, IdentifyCodeActivity.class);
                                        //  intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onError(Response<String> response) {
                                        Log.e("TAG", "onError: " + response.getException().toString());
                                        Intent intent = new Intent();
                                        intent.putExtra("phone", loginPhone.getText().toString().trim());
                                        intent.setClass(LoginActivity.this, IdentifyCodeActivity.class);
                                        //   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                        startActivity(intent);
                                    }

                                    @Override
                                    public void onStart(Request<String, ? extends Request> request) {
                                        super.onStart(request);
                                        //开始加载进度条
                                        pbProgress.setVisibility(View.VISIBLE);

                                        pbProgress.show();
                                        LogUtils.e(TAG, "ONSTART");
                                    }

                                    @Override
                                    public void onFinish() {
                                        super.onFinish();

                                        pbProgress.setVisibility(View.GONE);
                                        //结束进度条
                                        pbProgress.hide();

                                    }
                                });
                    } else {
                        Toast.makeText(LoginActivity.this, "请您输入正确的手机号码", Toast.LENGTH_LONG).show();
                    }
                }
                break;
            case R.id.login_weixin:


                String appId = CommonConstant.WECHAT_APPID; // 填应用AppId
                IWXAPI api = WXAPIFactory.createWXAPI(LoginActivity.this, appId);

                if (api != null && api.isWXAppInstalled()) {
                    SendAuth.Req req = new SendAuth.Req();
                    req.scope = "snsapi_userinfo";
                    req.state = "wechat_sdk_demo_test";
                    api.sendReq(req);
                } else {
                    Toast.makeText(LoginActivity.this, "若想使用该功能，请您先安装微信", Toast.LENGTH_LONG).show();

                }
                break;
        }
    }


}
